package com.greenant.store.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.greenant.store.AntHomeApplication;
import com.greenant.store.R;
import com.greenant.store.bean.OrderItem;
import com.greenant.store.utils.iLog;
import com.nostra13.universalimageloader.core.ImageLoader;

public class WeightGoodsListAdapter extends BaseAdapter {

	private List<OrderItem> list;
	private Context context;
	// BitmapUtils bitmapUtils;
	private int touchedPosition = 0;
	DecimalFormat df = new DecimalFormat("#########.##");

	private int MIN_MARK = 0;
	private int MAX_MARK = 10000000;

	public WeightGoodsListAdapter(List<OrderItem> orderItemList, Context ctx) {
		this.list = orderItemList;
		this.context = ctx;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.item_weight_goods, null);
			viewHolder = new ViewHolder();

			viewHolder.name = (TextView) convertView.findViewById(R.id.name);
			viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
			viewHolder.price = (TextView) convertView.findViewById(R.id.iv_googds_price);
			viewHolder.cost = (TextView) convertView.findViewById(R.id.iv_googds_discount);
			viewHolder.quantity = (TextView) convertView.findViewById(R.id.iv_googds_quantity);
			viewHolder.weight = (EditText) convertView.findViewById(R.id.iv_googds_weight);
			viewHolder.weightUnit = (TextView) convertView.findViewById(R.id.iv_googds_weight_unit);
			viewHolder.weightTitle = (TextView) convertView.findViewById(R.id.iv_googds_weight_title);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final OrderItem orderItem = list.get(position);
		final ViewHolder viewHolderTemp = viewHolder;
		viewHolderTemp.TAG = orderItem.getGoodsId();
		// orderItem.getName();
		viewHolder.name.setText(orderItem.getName());

		ImageLoader.getInstance().displayImage(orderItem.getPicUrl(), viewHolder.icon,
				AntHomeApplication.imageDisplayOptions);

		// 单价
		viewHolder.price.setText(context.getString(R.string.orderdetail_price,
				String.valueOf(orderItem.getPrice()) + "/" + orderItem.getUnit()));

		if (orderItem.isWeighAble()) {
			viewHolder.weight.setVisibility(View.VISIBLE);
			viewHolder.weightUnit.setText(orderItem.getUnit());
			if (orderItem.getWeight() < 0.000001) {
				// viewHolder.weight.setText("--.--");
				viewHolder.weight.setHint("--.--");
				// viewHolder.weight.setText("0");
				viewHolder.cost.setText(context.getString(R.string.orderdetail_cost, "--.--"));
			} else {
				viewHolder.weight.setText(String.valueOf(orderItem.getWeight()));
				viewHolder.cost.setText(context.getString(R.string.orderdetail_cost,
						String.valueOf(df.format(orderItem.getWeight() * orderItem.getPrice()))));
			}
			// 数量
			viewHolder.quantity.setText(context.getString(R.string.orderdetail_quantity,
					String.valueOf(orderItem.getQuantity()) + orderItem.getWeighUnit()));

			viewHolder.weight.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
					if (orderItem.getGoodsId() != null && orderItem.getGoodsId().equals(viewHolderTemp.TAG)) {
						if (start > 1) {
							if (MIN_MARK != -1 && MAX_MARK != -1) {
								try {
									int num = Integer.parseInt(s.toString());
									if (num > MAX_MARK) {
										s = String.valueOf(MAX_MARK);
									} else if (num < MIN_MARK) {
										s = String.valueOf(MIN_MARK);
									}

									viewHolderTemp.weight.setText(s);
								} catch (NumberFormatException e) {
									e.printStackTrace();
								}
							}
						}
						double weight = 0;
						try {
							weight = Double.parseDouble(s.toString());
						} catch (NumberFormatException e) {
							e.printStackTrace();
						}
						orderItem.setWeight(weight);

						for (int i = 0; i < list.size(); i++) {
							iLog.d("getWeight:" + list.get(i).getName() + ":" + list.get(i).getWeight());
						}
						viewHolderTemp.cost.setText(context.getString(R.string.orderdetail_cost,
								String.valueOf(df.format(orderItem.getWeight() * orderItem.getPrice()))));
					}

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start, int count, int after) {

				}

				@Override
				public void afterTextChanged(Editable s) {
					// // notifyDataSetChanged();
					// if (touchedPosition == position) {
					// // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
					// viewHolderTemp.weight.requestFocus();
					//
					// } else {
					// viewHolderTemp.weight.clearFocus();
					// }
				}
			});

			viewHolder.weight.setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_UP) {
						touchedPosition = position;
					}
					return false;
				}
			});
			viewHolder.weightTitle.setVisibility(View.VISIBLE);
		} else {

			viewHolder.weight.setVisibility(View.GONE);
			viewHolder.cost.setText(context.getString(R.string.orderdetail_cost,
					String.valueOf(df.format(orderItem.getQuantity() * orderItem.getPrice()))));

			// 数量
			viewHolder.quantity.setText(context.getString(R.string.orderdetail_quantity,
					String.valueOf(orderItem.getQuantity()) + orderItem.getUnit()));
			viewHolder.weightTitle.setVisibility(View.GONE);
		}

		return convertView;
	}

	class ViewHolder {

		public TextView name;
		public ImageView icon;
		public TextView price;
		public TextView cost;
		public TextView quantity;
		public EditText weight;
		public TextView weightUnit;
		public TextView weightTitle;

		public String TAG;

	}

}
